#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import StringIO
import os
import sys

execfile(os.path.join(sys.path[0], "framework.py"))

from oriolus.pith.packets.meta import Meta
from oriolus.pith.packets.metareader import MetaReader
from oriolus.pith.packets.metawriter import MetaWriter
from unittest import TestCase, TestLoader, TextTestRunner

class TestMeta(TestCase):
    def test_meta_reader1(self):
        packet = self.__get_metapacket_content1()
        string = StringIO.StringIO(packet)
        meta_reader = MetaReader()
        meta = meta_reader.read(string)

        self.assertEqual("test_packet", meta.name)
        self.assertEqual("1.2.3.4", meta.version)
        self.assertEqual("Some test", meta.description)

        self.assertEqual(len(meta.conflicts), 1)
        self.__check_named_version_range(meta.conflicts[0], {"Name": "test_packet_lake", "Version": "1.2.0.0"})

        self.assertEqual(len(meta.replaces), 1)
        self.__check_named_version_range(meta.replaces[0], {"Name": "test_packet", "Version": "(,10.0.0.1]"})

        self.assertEqual(len(meta.depends), 3)
        self.__check_named_version_range(meta.depends[0], {"Name": "foo_packet", "Version": "(1.2.3.0, 3.0.0]"})
        self.__check_named_version_range(meta.depends[1], {"Name": "fake_packet", "Version": "(, 1.2.3.4]"})
        self.__check_named_version_range(meta.depends[2], {"Name": "fake_packet2", "Version": "[1.0, 1.2]"})

    def test_meta_reader_writer1(self):
        stream = StringIO.StringIO()
        meta = Meta("Name",
                    "1.2.3.4",
                    "Description",
                    "[{Name: test_packet_lake, Version: 1.2.0.0}]",
                    "[{Name: test_packet, Version: (,10.0.0.1]}]",
                    "[{Name: fake_packet2, Version: [1.0, 1.2]}]")

        meta_writer = MetaWriter()
        meta_writer.write(meta, stream)
        stream.seek(0)

        meta_reader = MetaReader()
        meta2 = meta_reader.read(stream)

        self.assertEqual(meta.name, meta2.name)

    def test_meta_is_in_depends1(self):
        depends = [{"Name": "foo_packet", "Version": "(1.2.3.0, 3.0.0]"}, {"Name": "fake_packet2", "Version": "[1.0, 1.2]"}]
        meta = Meta("test", "1.2.3.4", None, [], [], depends)

        self.assertTrue(meta.is_in_depends("fake_packet2", "1.1.1.1"))

    def test_meta_is_depends2(self):
        depends = [{"Name": "fake_packet", "Version": "(, 1.2.3.4]"}, {"Name": "fake_packet2", "Version": "[1.0, 1.2]"}]
        meta = Meta("test", "1.2.3.4", None, [], [], depends)

        self.assertFalse(meta.is_in_depends("fake_packet2", "1.3.1.1"))
        self.assertFalse(meta.is_in_depends("fake_packet3", "1.1.1.1"))

    def test_meta_is_in_replaces1(self):
        replaces = [{"Name": "fake_packet2", "Version": "[1.0, 1.2]"}]
        meta = Meta("test", "1.2.3.4", None, [], replaces, [])

        self.assertTrue(meta.is_in_replaces("fake_packet2", "1.1.1.1"))

    def test_meta_is_replaces2(self):
        replaces = [{"Name": "fake_packet", "Version": "(, 1.2.3.4]"}, {"Name": "fake_packet2", "Version": "[1.0, 1.2]"}]
        meta = Meta("test", "1.2.3.4", None, [], replaces, [])

        self.assertFalse(meta.is_in_replaces("fake_packet2", "1.3.1.1"))
        self.assertFalse(meta.is_in_replaces("fake_packet3", "1.1.1.1"))

    def test_meta_is_in_conflicts1(self):
        conflicts = [{"Name": "fake_packet2", "Version": "[1.0, 1.2]"}]
        meta = Meta("test", "1.2.3.4", None, conflicts, [], [])

        self.assertTrue(meta.is_in_conflicts("fake_packet2", "1.1.1.1"))

    def test_meta_is_conflicts2(self):
        conflicts = [{"Name": "fake_packet", "Version": "(, 1.2.3.4]"}, {"Name": "fake_packet2", "Version": "[1.0, 1.2]"}]
        meta = Meta("test", "1.2.3.4", None, conflicts, [], [])

        self.assertFalse(meta.is_in_conflicts("fake_packet2", "1.3.1.1"))
        self.assertFalse(meta.is_in_conflicts("fake_packet3", "1.1.1.1"))

    def __check_named_version_range(self, actual_pair, expected_pair):
        self.assertEqual(actual_pair["Name"], expected_pair["Name"])
        self.assertEqual(actual_pair["Version"], expected_pair["Version"])

    def __get_metapacket_content1(self):
        packet1="""
name : test_packet
version : 1.2.3.4
description : Some test
conflicts : [{Name: test_packet_lake, Version: 1.2.0.0}]
replaces: [{Name: test_packet, Version: "(,10.0.0.1]"}]
depends:
- Name: foo_packet
  Version: "(1.2.3.0, 3.0.0]"
- Name: fake_packet
  Version: "(, 1.2.3.4]"
- Name: fake_packet2
  Version: "[1.0, 1.2]" """

        return packet1

if __name__ == "__main__":
    suite = TestLoader().loadTestsFromTestCase(TestMeta)
    TextTestRunner(verbosity=2).run(suite)
